﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class TopicDAO
    {
        public static List<TOPIC> getAllTopics()
        {
            List<TOPIC> res = null;
            try
            {
                DataClassesMyVocabularyDataContext db = new DataClassesMyVocabularyDataContext();
                res = db.usp_SelectTopicsAll().ToList();
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }

        public static TOPIC getTopic(int idtopic)
        {
            TOPIC res = null;
            try
            {
                DataClassesMyVocabularyDataContext db = new DataClassesMyVocabularyDataContext();
                List<TOPIC> tempRes = db.usp_SelectTopicByPrimaryKey(idtopic).ToList();
                if (tempRes.Count > 0)
                {
                    res = tempRes[0];
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }

        public static TOPIC getTopic(String name)
        {
            TOPIC res = null;
            try
            {
                DataClassesMyVocabularyDataContext db = new DataClassesMyVocabularyDataContext();
                List<TOPIC> tempRes = db.usp_SelectTopicByName(name).ToList();
                if (tempRes.Count > 0)
                {
                    res = tempRes[0];
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }

        public static bool Add(TOPIC tp)
        {
            bool res = false;
            try
            {
                DataClassesMyVocabularyDataContext db = new DataClassesMyVocabularyDataContext();
                db.TOPICs.InsertOnSubmit(tp);
                db.SubmitChanges();
                res = true;
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }

        public static bool Edit(int idtopic, TOPIC tpUpdate)
        {
            bool res = false;
            try
            {
                DataClassesMyVocabularyDataContext db = new DataClassesMyVocabularyDataContext();
                TOPIC tpNew = getTopic(idtopic);
                tpNew = tpUpdate;
                db.SubmitChanges();
                res = true;
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }

        public static bool Remove(TOPIC tp)
        {
            bool res = false;
            try
            {
                DataClassesMyVocabularyDataContext db = new DataClassesMyVocabularyDataContext();
                db.TOPICs.DeleteOnSubmit(tp);
                db.SubmitChanges();
                res = true;
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }
    }
}
